package service

import (
	"context"
	pb "digitalhuman/api/assets/v1"
	"digitalhuman/internal/data"
)

type AssetsService struct {
	pb.UnimplementedAssetsServer
	AssData *data.AssetData
}

func NewAssetsService(ad *data.AssetData) *AssetsService {
	return &AssetsService{AssData: ad}
}

func (s *AssetsService) GetBackgroundList(ctx context.Context, req *pb.GetAssetsRequest) (*pb.GetAssetListReply, error) {
	assets, err := s.AssData.FindListByType(ctx, int(pb.AssetTypeEnum_BACKGROUND_IMAGE.Number()))
	if err != nil {
		return nil, err
	}

	replyAssets := make([]*pb.Asset, len(assets))
	for i, asset := range assets {
		assetMap := s.AssData.FindAssetFromAssetMap(asset.ID)
		if asset.Thumbnail != "" && assetMap != nil {
			asset.Thumbnail = s.AssData.FindAssetFromAssetMap(asset.ID).Thumbnail // 拼装了带有oss的url
		}
		replyAssets[i] = &pb.Asset{
			Id:         int32(asset.ID),
			Name:       asset.Name,
			Payload:    asset.Payload,
			Size:       int32(asset.Size),
			Type:       int32(asset.Type),
			Thumbnail:  asset.Thumbnail,
			Permission: 1, // todo permission 1
		}
	}

	return &pb.GetAssetListReply{Assets: replyAssets}, nil
}
func (s *AssetsService) GetHumanList(ctx context.Context, req *pb.GetAssetsRequest) (*pb.GetAssetListReply, error) {
	assets, err := s.AssData.FindListByType(ctx, int(pb.AssetTypeEnum_HUMAN))
	if err != nil {
		return nil, err
	}

	replyAssets := make([]*pb.Asset, len(assets))
	for i, asset := range assets {
		assetMap := s.AssData.FindAssetFromAssetMap(asset.ID)
		if asset.Thumbnail != "" && assetMap != nil {
			asset.Thumbnail = s.AssData.FindAssetFromAssetMap(asset.ID).Thumbnail // 拼装了带有oss的url
		}
		replyAssets[i] = &pb.Asset{
			Id:         int32(asset.ID),
			Name:       asset.Name,
			Payload:    asset.Payload,
			Size:       int32(asset.Size),
			Type:       int32(asset.Type),
			Thumbnail:  asset.Thumbnail,
			Permission: 1, // todo permission 1
		}
	}
	return &pb.GetAssetListReply{Assets: replyAssets}, nil
}
